package com.cap.dreamcircle.View.Adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cap.dreamcircle.Consts.Constants;
import com.cap.dreamcircle.Model.Action.CommentsAction;
import com.cap.dreamcircle.Model.Bean.CommentEntity;
import com.cap.dreamcircle.R;
import com.cap.dreamcircle.Utils.GlideUtils;
import com.cap.dreamcircle.Utils.ViewHolderUtils;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by liucxu on 2017/6/27.
 */

public class AdapterDreamDetailsComments extends BaseAdapter {

    private Context context;
    private List<CommentEntity> datas = new ArrayList<>();

    public AdapterDreamDetailsComments(Context c, List<CommentEntity> entities) {
        context = c;
        if (entities != null) {
            datas.clear();
            datas.addAll(entities);
        }
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setDatas(List<CommentEntity> entities) {
        if (entities != null) {
            datas.clear();
            datas.addAll(entities);
            notifyDataSetChanged();
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.adapter_dream_details_comment, null);
        }
        initViews(convertView,position);
        return convertView;
    }

    private void initViews(View view, final int position){
        CircleImageView comment_circle_header_imv= ViewHolderUtils.get(view,R.id.comment_circle_header_imv);
        TextView tv_comment_user_name=ViewHolderUtils.get(view,R.id.tv_comment_user_name);
        TextView tv_dream_comments_content=ViewHolderUtils.get(view,R.id.tv_dream_comments_content);
        GlideUtils.getInstance().LoadContextCircleBitmap(context,"",comment_circle_header_imv);
        GlideUtils.getInstance().LoadContextBitmap(context, Constants.BASE_URL+datas.get(position).getHeadImgUrl(),comment_circle_header_imv,R.mipmap.default_header,R.mipmap.default_header,GlideUtils.LOAD_BITMAP);
        if(TextUtils.isEmpty(datas.get(position).getToUserName())){
            tv_comment_user_name.setText(datas.get(position).getNickName());
        }
        else{
            tv_comment_user_name.setText(datas.get(position).getNickName()+" 回复 "+datas.get(position).getToUserName()+" :");
        }
        tv_dream_comments_content.setText(datas.get(position).getCommentContent());
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommentsAction action=new CommentsAction();
                action.setToUserId(datas.get(position).getUserId());
                action.setPosition(position);
                EventBus.getDefault().post(action);
            }
        });
    }
}
